﻿using System;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;
using System.Collections;

namespace Acms.Bll
{
    public class CustomAdminPage : BaseItem
    {
        private CustomAdminPage() : base() { }
        private CustomAdminPage(Hashtable table) : base(table) { }
        private CustomAdminPage(Hashtable table, List<CustomAdminPage> collection) : base(table, collection) { }

        public static CustomAdminPage CreateInstance(int siteId)
        {
            CustomAdminPage tmp = new CustomAdminPage();
            tmp.SiteId = siteId;
            return tmp;
        }

        protected override Acms.SqlDal.Base CreateDal()
        {
            return new CustomAdminPageDal();
        }

        private static CustomAdminPage Get(Hashtable table)
        {
            if (table != null)
            {
                return new CustomAdminPage(table);
            }
            else
            {
                return null;
            }
        }

        private static List<CustomAdminPage> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<CustomAdminPage> tmp = new List<CustomAdminPage>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new CustomAdminPage(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        protected override void Load(System.Collections.Hashtable table)
        {
            Id = (int)table[CustomAdminPageDal.Fields.Id];
            SiteId = (int)table[CustomAdminPageDal.Fields.SiteId];
            Name = (string)table[CustomAdminPageDal.Fields.Name];
            ControlPath = (string)table[CustomAdminPageDal.Fields.ControlPath];
            Notes = (string)table[CustomAdminPageDal.Fields.Notes];
            Sequence = (int)table[CustomAdminPageDal.Fields.Sequence];
            Section = (Sections)table[CustomAdminPageDal.Fields.Section];
        }

        public enum Sections : byte
        {
            Data = 1,
            Settings = 2,
            Unknown = 0
        }

        public int Id { get; private set; }
        public int SiteId { get; private set; }
        public string Name { get; set; }
        public string ControlPath { get; set; }
        public string Notes { get; set; }
        public int Sequence { get; private set; }
        public Sections Section { get; set; }

        public static CustomAdminPage GetById(int id)
        {
            CustomAdminPageDal dal = new CustomAdminPageDal();
            return Get(dal.GetById(id));
        }

        public static CustomAdminPage GetByNameAndSiteId(string name, int siteId)
        {
            CustomAdminPageDal dal = new CustomAdminPageDal();
            return Get(dal.GetByNameAndSiteId(name, siteId));
        }


        public static List<CustomAdminPage> GetAllBySiteId(int siteId)
        {
            CustomAdminPageDal dal = new CustomAdminPageDal();
            return Get(dal.GetAllBySiteId(siteId));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            int idOut = Id;
            int sequenceOut = Sequence;
            ((CustomAdminPageDal)dal).Update(ref idOut, SiteId, Name, ControlPath, Notes, ref sequenceOut, (byte)Section, ref concurencyId);
            Id = idOut;
            Sequence = sequenceOut;
            return Id;
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((CustomAdminPageDal)dal).Delete(Id, concurencyId);
        }



        public static void Move(List<CustomAdminPage> collection, int movingIndex, int replacingIndex)
        {
            //get the moving
            CustomAdminPage moving = collection[movingIndex];
            //get the move to
            CustomAdminPage replacing = collection[replacingIndex];

            bool saveAll = false;
            if (moving.Sequence == replacing.Sequence)
            {
                saveAll = true;
                for (int i = 0; i < collection.Count; i++)
                {
                    if (collection[i].Sequence != i)
                    {
                        collection[i].Sequence = i;
                    }
                }
            }

            int tmpSequence = moving.Sequence;
            moving.Sequence = replacing.Sequence;
            replacing.Sequence = tmpSequence;

            if (saveAll)
            {
                collection[0].BeginTransaction();
                foreach (CustomAdminPage t in collection)
                {
                    if (t != collection[0])
                    {
                        t.UseTransaction(collection[0].TransactionKey);
                    }
                    t.Update();
                }
                collection[0].CommitTransaction();
            }
            else
            {
                moving.BeginTransaction();
                replacing.UseTransaction(moving.TransactionKey);
                moving.Update();
                replacing.Update();
                moving.CommitTransaction();
            }

        }


    }
}
